﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XAJH
{
    /// <summary>
    /// 键盘帮助类
    /// </summary>
    public class KeyboardHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bVk"> 定义一个虚拟键码。键码值必须在1～254之间。 </param>
        /// <param name="bScan"> 定义该键的硬件扫描码。(一般为0  )</param>
        /// <param name="dwFlags"> 定义函数操作的各个方面的一个标志位集。( 0为按下，2为释放) </param>
        /// <param name="dwExtralnfo"> 定义与击键相关的附加的32位值。(一般情况下设成为0) </param>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "keybd_event", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private extern static void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtralnfo);

        /// <summary>
        /// 键盘
        /// </summary>
        /// <param name="key"></param>
        /// <param name="delay"></param>
        public static void KeyPress(System.Windows.Forms.Keys key, int delay = 0)
        {
            //  键盘按下
            keybd_event((byte)key, 0, 0, 0);
            if (delay > 0)
                System.Threading.Thread.Sleep(delay);
            //  键盘弹起
            keybd_event((byte)key, 2, 0, 0);
        }
    }
}
